package br.com.portalecbahia.connectionservices;


import android.content.Context;
import android.os.AsyncTask;
import android.telephony.TelephonyManager;
import android.util.Log;
import br.com.portalecbahia.GCMIntentService;
import br.com.portalecbahia.listner.NotificationListeners;
import br.com.portalecbahia.util.PortalECBahiaUtil;

public class SynchronizeNotification extends AsyncTask<Boolean, Void, Boolean> {
	
	private Context context;
	private NotificationListeners listeners;
	private br.com.portalecbahia.connectionservices.HTTPConnect connect;
	public SynchronizeNotification(Context context){
	
		this.context = context;
		
		this.listeners = (NotificationListeners)context;
	}
	@Override
	protected void onPreExecute() {
	
	}
	
  
    @Override
    protected void onPostExecute(Boolean result) {               
    	listeners.onSynchronizeNotificationComplete(result);
    
    }

    @Override
	protected Boolean doInBackground(Boolean... params) {
		try{
			Boolean enable = params[0];
			if(PortalECBahiaUtil.haveNetworkConnection(context)){
	
				if(enable){
					connect = new HTTPConnect(PortalECBahiaUtil.urlServer+"/InsertDeviceNotification?id="+GCMIntentService.id+"&imei="+getImei());
		            connect.getObject("", null);
	
				}else{
					connect = new HTTPConnect(PortalECBahiaUtil.urlServer+"/DeleteDeviceNotification?imei="+getImei());
		            connect.getObject("", null);
	
	
				}
        	}
		
		}catch (Exception e) {
			Log.e(PortalECBahiaUtil.TAG, e.toString());
			return  false;
		}
		return true;
		
	}
	private String getImei() {
		TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getDeviceId();

	}
}
